﻿#include "tfflickviewer.h"
#include <QtMath>
#include <QScrollBar>

TFFlickViewer::TFFlickViewer(QWidget *parent) : QWidget(parent)
{
    mScrollArea = new QScrollArea(this);

    connect(mScrollArea->verticalScrollBar(),&QScrollBar::sliderMoved,this,[=](int position){
        qDebug()<<"int position "<<position;
    });
    mBgWidget = new QWidget(mScrollArea);
    mScrollArea->setWidget(mBgWidget);
    initData(20);
}

void TFFlickViewer::resizeEvent(QResizeEvent *event)
{
    mBgWidget->setFixedWidth(event->size().width());
    mScrollArea->resize(event->size());
    updateLbs();
    changed();
}

void TFFlickViewer::wheelEvent(QWheelEvent *event)
{
    QPoint numDegrees = event->angleDelta() / 8;
    QPoint numSteps   = 4 * numDegrees; // 60s  // 60 * numDegrees / 15 ;

    int value = mScrollArea->verticalScrollBar()->value() - numSteps.y();
//    m_verScrollBar->setValue(value);
    qDebug()<<"wheelEvent position "<<value;
    if(value>=0){
        ItemInfo info;
        int cnt = mItemList.size();
        for(int i=0;i<3;i++){
            info.text = QString::number(i);
            info.url = QString("D:/examples/MixQmlWidget/images/1 (%1).gif").arg(i+cnt);
            QLabel *lb = new QLabel(info.text);
            lb->setToolTip(info.text);
            lb->setParent(mBgWidget);
            QPixmap pix(info.url);
            lb->setPixmap(pix);
            lb->setScaledContents(true);
            lb->setFixedWidth(200);
            lb->setFixedHeight(pix.size().height());
            lb->setVisible(false);
            info.lb = lb;
            mItemList.append(info);
        }
        changed();
        mScrollArea->verticalScrollBar()->setValue(mScrollArea->verticalScrollBar()->maximum());
    }
    event->accept();
}
int TFFlickViewer::findMin(const QList<int> &arr) {
    int m = 0;
    for (int i = 0; i < arr.length(); i++) {
        m = qMin(arr[m], arr[i]) == arr[m] ? m : i;
    }
    return m;
}
void TFFlickViewer::changed()
{
    if(mItemList.isEmpty())return;
    int lis_W= this->width()/perCount;
    int windowCW = this->width();
    int n = qFloor(windowCW/lis_W);
    int center = (windowCW - n*lis_W-(n-1)*10)/2;
    QList<int> arrH;
    for(int i=0;i<mItemList.count();i++){
        int j = i%n;
        if (arrH.length() == n) {
            int min = findMin(arrH);
            mItemList[i].pos.setX(center + min*(lis_W+10)) ;
            mItemList[i].pos.setY( arrH[min]+10 );
            arrH[min] += mItemList.at(i).lb->height() + 10;
            int k=0;
            for(int b=0;b<n;b++){
                k = qMax(arrH[k],arrH[b]) == arrH[k] ? k : b;
            }
            mBgWidget->setFixedHeight(arrH[k]);
            qDebug()<<" index:"<<i<<" postion: "<<arrH[k]<<endl;
        }else{
            arrH.insert(j, mItemList.at(i).lb->height()) ;
            mItemList[i].pos.setX(center + lis_W*j+10*j ) ;
            mItemList[i].pos.setY(0 );
        }
        mItemList[i].lb->setVisible(true);
        mItemList[i].lb->move(mItemList.at(i).pos);
    }
    loadFinshed = true;
}

void TFFlickViewer::updateLbs()
{
    int lis_W= this->width()/perCount;
    for(int i=0;i<mItemList.size();++i){
        mItemList[i].lb->setFixedWidth(lis_W);
    }
}

void TFFlickViewer::initData(int cnt)
{
    ItemInfo info;
    for(int i=0;i<cnt;++i){
        info.text = QString::number(i);
        info.url = QString("D:/examples/MixQmlWidget/images/1 (%1).gif").arg(i+1);
        QLabel *lb = new QLabel(info.text);
        lb->setToolTip(info.text);
        lb->setParent(mBgWidget);
        QPixmap pix(info.url);
        lb->setPixmap(pix);
        lb->setScaledContents(true);
        lb->setFixedWidth(200);
        lb->setFixedHeight(pix.size().height());
        lb->setVisible(false);
        info.lb = lb;
        mItemList.append(info);
    }
}
